
extends RigidBody2D
class_name Brick

@onready var area_2d: Area2D = $Area2D

func _ready() -> void:
	if Global.current_stage_name == "stage5":
		var timer = Timer.new()
		timer.wait_time = 9
		timer.autostart = true
		timer.one_shot = true
		add_child(timer)
		timer.timeout.connect(queue_free)
		return
	area_2d.body_entered.connect(on_body_entered)

func bounce(direction: Vector2) -> void:
	# 反弹速度与原速度模长一致，方向为反射方向
	var speed = linear_velocity.length()
	if speed < 1.0:
		speed = 200.0 # 静止时给个基础速度，防止卡住
	linear_velocity = direction.normalized() * speed


func on_body_entered(body: Player) -> void:
	body.die()

func die() -> void:
	queue_free()
